<?php (defined('BASEPATH')) OR exit('No direct script access allowed');

class Report_Model extends CI_Model {
	
	private $table= '';
	function __construct() {
		parent::__construct();
		$this->shiftingModel = $this->load->model('Shifting_Model');
		$this->clientModel = $this->load->model('Client_Model');
		$this->userModel = $this->load->model('User_Model');
		$this->load->helper('DateUtils');
		$this->load->helper('ConstantsUtils');
	}
	
	public function getPayrollJournalData($api, $payroll_period_id, $user_id = 0){
		$this->db->select("fp.*, fp.sss as `sss_cont`, fp.hdmf as `hdmf_cont`, fp.philhealth as `philhealth_cont`, p.regular_hours as p_regular_hours,
		p.regular_overtime as p_regular_overtime, p.regular_night_differential as p_regular_night_differential,
		p.restday as p_restday, p.excess_restday as p_excess_restday, p.restday_night_differential as p_restday_night_differential,
		p.special_holiday as p_special_holiday, p.excess_special_holiday as p_excess_special_holiday, p.special_holiday_night_differential as 
		p_special_holiday_night_differential, p.special_holiday_restday as p_special_holiday_restday, p.excess_special_holiday_restday as p_excess_special_holiday_restday,
		p.legal_holiday as p_legal_holiday, p.excess_legal_holiday as p_excess_legal_holiday, p.legal_holiday_unwork as p_legal_holiday_unwork,
		p.legal_holiday_restday as p_legal_holiday_restday, p.excess_legal_holiday_restday as p_excess_legal_holiday_restday,
		p.excess_legal_holiday_restday as p_excess_legal_holiday_restday, p.legal_holiday_night_differential as p_legal_holiday_night_differential,
		p.adjustment as p_adjustment, p.incentives as p_incentives, p.approved_undertime as p_approved_undertime,
		p.approved_late as p_approved_late, p.tax as p_tax, p.sss as p_sss, p.hdmf as p_hdmf, p.philhealth as p_philhealth, p.sss_loan as p_sss_loan,
		p.hdmf_loan as p_hdmf_loan, f.*, p.canteen as p_canteen, p.uniform as p_uniform");
		$this->db->from('final_payroll as fp');
		$this->db->join('payroll as p','fp.pid=p.id','inner');
		$this->db->join('file201 as f','p.user_id=f.user_id','left');
		$this->db->where('p.payroll_period_id', $payroll_period_id); 
		if($user_id > 0){
			$this->db->where('p.user_id', $user_id); 
		}
		$query = $this->db->get();
		$result = $query->result_array();
		foreach($result as &$data){
			$user_rate = $this->userModel->getUserRate($data['user_id'],'array');
			//var_dump($user_rate);
			$data['user_rates'] = $user_rate[0];
		}
		return $result;
		
	}
	
	public function getEmployeeTimesheetData($api,$payroll_period_id, $user_id) {
		
		$payroll = $this->getObject('payroll_period', array('id' => $payroll_period_id));
		
		$this->db->select('t.*');
		$this->db->from('timesheets as t');
		//$this->db->where('time_in', $payroll_period_id);
		$this->db->where('t.date >=', $payroll->date_start);
		$this->db->where('t.date <=', $payroll->date_end);
		//$this->db->where('t.time_in !=', ConstantsUtils::$EMPTY_DATE_TIME);
		//$this->db->where('t.time_out !=', ConstantsUtils::$EMPTY_DATE_TIME);
		$this->db->where('t.user_id', $user_id);
		//$this->db->where('t.remarks !=', 5);
		//$this->db->where('t.remarks !=', 1);
		$this->db->where('t.is_saved', 1);
		$query = $this->db->get();
		$result = $query->result_array();
		$client = $this->clientModel->find($api);
		
		$remarks_exception = array(1,5);
		foreach ($result as &$data) {
			
			$rest_day = ($this->userModel->isUserRestDay($api, $user_id, $data['date']) && !in_array($data['remarks'],$remarks_exception))?8:0;
			$excess_hours = DateUtils::time_to_decimal($data['approved_overtime'])/60;
			$night_diff = DateUtils::time_to_decimal($data['approved_night_differential'])/60;
			$rest_day_night_diff = $this->userModel->isUserRestDay($api, $user_id, $data['date'])?$night_diff:0;
			$params = array('client_id'=>$client->id, 
			'date_start'=>$data['date'], 
			'date_end'=>$data['date']);
			
			$data['approved_overtime'] = DateUtils::time_to_decimal($data['approved_overtime'])/60;
			$data['approved_undertime'] = DateUtils::time_to_decimal($data['approved_undertime'])/60;
			$data['approved_night_differential'] = $night_diff;
			$data['date'] = substr($data['date'],5,5);
			$data['time_in'] = substr($data['time_in'],11,5);
			$data['time_out'] = substr($data['time_out'],11,5);
			$isHoliday = $this->clientModel->getClientHolidays($params);
			//var_dump($isHoliday);
			
			if(!empty($isHoliday)){
				
				switch($isHoliday[0]->holiday_type){
					case 1:
						$legal_holiday = !in_array($data['remarks'],$remarks_exception)?8:0;
						if($excess_hours > 0) {
							$excess_legal_holiday = $excess_hours;
							if($rest_day > 0) {
								$excess_legal_holiday_rest_day = 8;
							}
						}
						if($night_diff > 0){
							$legal_holiday_night_differential = $night_diff;
							if($rest_day > 0) {
								$legal_holiday_rest_day_night_differential = $night_diff;
							}
						}
						
						if($legal_holiday > 0) {
							if($rest_day > 0) {
								$legal_holiday_rest_day = 8;
							}
						}
						break;
					case 2:
						$special_holiday = !in_array($data['remarks'],$remarks_exception)?8:0;
						if($excess_hours > 0) {
							$excess_special_holiday = $excess_hours;
							if($rest_day > 0) {
								$excess_special_holiday_rest_day = 8;
							}
						}
						if($night_diff > 0){
							$special_night_differential = $night_diff;
							if($rest_day > 0) {
								$special_holiday_rest_day_night_differential = $night_diff;
							}
						}
						if($special_holiday > 0) {
							if($rest_day > 0) {
								$special_holiday_rest_day = 8;
							}
						}
						break;
					case 3:
						break;
					case 4:
						break;
				}
			}else{
				$special_holiday = 0;
				$excess_special_holiday = 0;
				$special_holiday_night_differential = 0;
				$special_holiday_rest_day = 0;
				$excess_special_holiday_rest_day = 0;
				$special_holiday_rest_day_night_differential = 0;
				$legal_holiday = 0;
				$excess_legal_holiday = 0;
				$legal_holiday_night_differential = 0;
				$legal_holiday_rest_day = 0;
				$excess_legal_holiday_rest_day = 0;
				$legal_holiday_rest_day_night_differential = 0;
				
			}
			//var_dump($isHoliday);
			
			$data['regular_hours'] = !in_array($data['remarks'],$remarks_exception)?8:0;;
			$data['rest_day'] = $rest_day;
			$data['excess_rest_day'] = $excess_hours;
			$data['rest_day_night_differential'] = $rest_day_night_diff;
			
			$data['special_holiday'] = isset($special_holiday)?$special_holiday:0;
			$data['excess_special_holiday'] = isset($excess_special_holiday)?$excess_special_holiday:0;
			$data['special_holiday_night_differential'] = isset($special_night_differential)?$special_night_differential:0;
			
			$data['special_holiday_rest_day'] = isset($special_holiday_rest_day)?$special_holiday_rest_day:0;
			$data['excess_special_holiday_rest_day'] = isset($excess_special_holiday_rest_day)?$excess_special_holiday_rest_day:0;
			$data['special_holiday_rest_day_night_differential'] = isset($special_holiday_rest_day_night_differential)?$special_holiday_rest_day_night_differential:0;
			
			$data['legal_holiday'] = isset($legal_holiday)?$legal_holiday:0;
			$data['excess_legal_holiday'] = isset($excess_legal_holiday)?$excess_legal_holiday:0;
			$data['legal_holiday_night_differential'] = isset($legal_holiday_night_differential)?$legal_holiday_night_differential:0;
			
			$data['legal_holiday_rest_day'] = isset($legal_holiday_rest_day)?$legal_holiday_rest_day:0;
			$data['excess_legal_holiday_rest_day'] = isset($excess_legal_holiday_rest_day)?$excess_legal_holiday_rest_day:0;
			$data['legal_holiday_rest_day_night_differential'] = isset($legal_holiday_rest_day_night_differential)?$legal_holiday_rest_day_night_differential:0;
			
			$data['legal_holiday_unwork'] = 0;
			
			
		} 
		
		return $result;
		
	}
	
	private function getObject($table, $filter=array()){
		$query = $this->db->get_where($table,$filter,1,0);
		$result = $query->result();
		if(!empty($result)){
			return $result[0];
		}else {
			return false;
		}
	}
	
	/*public function getPaySlip($api, $payroll_period_id, $user_id){
		$this->db->select("fp.*, fp.sss as `sss_cont`, fp.hdmf as `hdmf_cont`, fp.philhealth as `philhealth_cont`, p.regular_hours as p_regular_hours,
		p.regular_overtime as p_regular_overtime, p.regular_night_differential as p_regular_night_differential,
		p.restday as p_restday, p.excess_restday as p_excess_restday, p.restday_night_differential as p_restday_night_differential,
		p.special_holiday as p_special_holiday, p.excess_special_holiday as p_excess_special_holiday, p.special_holiday_night_differential as 
		p_special_holiday_night_differential, p.special_holiday_restday as p_special_holiday_restday, p.excess_special_holiday_restday as p_excess_special_holiday_restday,
		p.legal_holiday as p_legal_holiday, p.excess_legal_holiday as p_excess_legal_holiday, p.legal_holiday_unwork as p_legal_holiday_unwork,
		p.legal_holiday_restday as p_legal_holiday_restday, p.excess_legal_holiday_restday as p_excess_legal_holiday_restday,
		p.excess_legal_holiday_restday as p_excess_legal_holiday_restday, p.legal_holiday_night_differential as p_legal_holiday_night_differential,
		p.adjustment as p_adjustment, p.incentives as p_incentives, p.approved_undertime as p_approved_undertime,
		p.approved_late as p_approved_late, p.tax as p_tax, p.sss as p_sss, p.hdmf as p_hdmf, p.philhealth as p_philhealth, p.sss_loan as p_sss_loan,
		p.hdmf_loan as p_hdmf_loan, p.canteen as p_canteen, f.*");
		$this->db->from('final_payroll as fp');
		$this->db->join('payroll as p','fp.pid=p.id','inner');
		$this->db->join('file201 as f','p.user_id=f.user_id','left');
		$this->db->where('p.payroll_period_id', $payroll_period_id); 
		if($user_id > 0){
			$this->db->where('p.user_id', $user_id); 
		}
		//$this->db->where('c.code', $code); 
		//$this->db->where('t.time_in >=', $date_start);
		//$this->db->where('t.time_out <=', $date_end);
		$query = $this->db->get();
		//$result = $query->result();
		$result = $query->result_array();
		foreach($result as &$data){
			$user_rate = $this->userModel->getUserRate($data['user_id'],'array');
			//var_dump($user_rate);
			$data['user_rates'] = $user_rate[0];
		}
		return $result;
	}*/
	
}